package com.zdkj.edge.door.demo.handler;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.spicdt.iedge.psdk.EdgeService;
import com.spicdt.iedge.psdk.data.CommandData;
import com.spicdt.iedge.psdk.data.CommandResponse;
import com.spicdt.iedge.psdk.data.Response;
import com.spicdt.iedge.psdk.handler.CommandHandler;
import com.spicdt.iedge.psdk.meta.DeviceCmdConstraint;
import com.zdkj.edge.door.demo.conf.AdmsConf;
import com.zdkj.edge.door.demo.model.ProtocolAuthInfo;
import com.zdkj.edge.door.demo.service.AdmsApiService;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class AdmsCommandHandler implements CommandHandler {
    EdgeService edgeService;
    AdmsApiService admsApiService;
    AdmsConf admsConf;

    public AdmsCommandHandler(AdmsApiService vehicleBaseService, EdgeService edgeService, AdmsConf config) {
        this.admsApiService = vehicleBaseService;
        this.edgeService = edgeService;
        this.admsConf = config;
    }

    @Override
    public void onCommand(String deviceKey, CommandData commandData) {
        log.info(" RPC received: device='{}', command={},params:{}", deviceKey, commandData.getMethod(), commandData.getParams());

        if (ObjectUtil.isEmpty(deviceKey)) {
            log.warn("No device '{}' found for RPC {}", deviceKey, commandData);
            edgeService.channelError("", new RuntimeException("deviceKey is null"));
            return;
        }
        Response results = null;
        JSONObject params = JSON.parseObject(commandData.getParams(), JSONObject.class);
        if (!params.containsKey("deviceCode") || ObjectUtil.isEmpty(params.getString("deviceCode"))) {
            params.put("deviceCode", deviceKey);
        }
        CommandResponse response = new CommandResponse();
        switch (commandData.getMethod()) {
            case DeviceCmdConstraint.DOOR_ADDPERSON:  //新增人员
                try {
                    ProtocolAuthInfo authInfo = JSONUtil.toBean(params.toJSONString(), ProtocolAuthInfo.class);
                    results = admsApiService.addPersonAuth(authInfo);
                    response.setData(JSONObject.toJSONString(results));
                } catch (Exception e) {
                    log.error("新增车辆授权异常:{}", e.getMessage(), e);
                    results = Response.fail("500", e.getMessage(), null);
                }
                break;

            case DeviceCmdConstraint.DEVICE_ADD_GRANT:  //新增人员授权
                try {
                    ProtocolAuthInfo authInfo = JSONUtil.toBean(params.toJSONString(), ProtocolAuthInfo.class);
                    results = admsApiService.addGrantAuth(authInfo);
                    response.setData(JSONObject.toJSONString(results));
                } catch (Exception e) {
                    log.error("新增车辆授权异常:{}", e.getMessage(), e);
                    results = Response.fail("500", e.getMessage(), null);
                }
                break;
            case DeviceCmdConstraint.DOOR_doorAccess:  //门禁控制
                try {
                    ProtocolAuthInfo authInfo = JSONUtil.toBean(params.toJSONString(), ProtocolAuthInfo.class);
                    results = admsApiService.doorAcces(authInfo);
                    response.setData(JSONObject.toJSONString(results));
                } catch (Exception e) {
                    log.error("新增车辆授权异常:{}", e.getMessage(), e);
                    results = Response.fail("500", e.getMessage(), null);
                }
                break;
            case DeviceCmdConstraint.DOOR_QUERY_PERSONLIST:
                results = admsApiService.personList(params);
                break;
            default:
                log.warn("Unknown RPC method '{}'", commandData);
                results = Response.fail("500", "Unknown RPC method '{}'" + commandData.getMethod(), null);
                break;
        }
        response.setData(JSONObject.toJSONString(results));
        response.setDeviceKey(deviceKey);
        response.setRequestId(commandData.getRequestId());
        log.info("onCommand:[{}]", JSONObject.toJSONString(results));

        edgeService.deviceCommandResponse(response);
    }
}
